package com.dream.tvsearcher.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dream.tvsearcher.R;
import com.dream.tvsearcher.bean.SearchBean;
import com.dream.tvsearcher.widget.FlowLayout;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


/**
 * Created by Vitcor Lam on 17/01/12/0012.
 */

public class MyFragment extends Fragment {
    LayoutInflater inflater;
    SearchBean bean;
    TextView tv_title,tv_year,tv_area,tv_desc,tv_acts;
    FlowLayout flow_tag,flow_act,flow_links,flow_rec,flow_acts,flow_dir;
    ImageView iv_cover;
    LinearLayout ll;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        bean= (SearchBean) getArguments().get(SearchBean.BEANKEY);
        this.inflater=inflater;
        return inflater.inflate(R.layout.fragment_main,container,false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        tv_title= (TextView) view.findViewById(R.id.tv_title);
        tv_year= (TextView) view.findViewById(R.id.tv_year);
        tv_area= (TextView) view.findViewById(R.id.tv_area);
        tv_desc= (TextView) view.findViewById(R.id.tv_desc);
        tv_acts= (TextView) view.findViewById(R.id.tv_acts);
        flow_dir= (FlowLayout) view.findViewById(R.id.flow_dir);
        flow_tag= (FlowLayout) view.findViewById(R.id.flow_tag);
        flow_act= (FlowLayout) view.findViewById(R.id.flow_act);
        flow_links= (FlowLayout) view.findViewById(R.id.flow_links);
        flow_rec= (FlowLayout) view.findViewById(R.id.flow_rec);
        flow_acts= (FlowLayout) view.findViewById(R.id.flow_acts);
        iv_cover= (ImageView) view.findViewById(R.id.iv_cover);
        ll= (LinearLayout) view.findViewById(R.id.ll_acts);
        TextView tv;
        if (!bean.getReason().equals("查询成功")){
            Snackbar.make(view,"对不起，没有找到该影片",Snackbar.LENGTH_SHORT).show();
            return;
        }
        SearchBean.ResultBean result = bean.getResult();
        tv_title.setText(result.getTitle());
        String tags = result.getTag();
        String[] split = tags.split(" / ");
        for (int i = 0; i < split.length; i++) {
            tv= (TextView) inflater.inflate(R.layout.search_label_tv,flow_tag,false);
            tv.setText(split[i]);
            flow_tag.addView(tv);
        }
        String[] split1 = result.getAct().split(" ");
        for (int i = 0; i < split1.length; i++) {
            tv= (TextView) inflater.inflate(R.layout.search_label_tv,flow_act,false);
            tv.setText(split1[i]);
            flow_act.addView(tv);
        }
        tv_year.setText(result.getYear());
        tv_area.setText(result.getArea());
        String[] split2 = result.getDir().split(" / ");
        for (int i = 0; i < split2.length; i++) {
            tv= (TextView) inflater.inflate(R.layout.search_label_tv,flow_dir,false);
            tv.setText(split2[i]);
            flow_dir.addView(tv);
        }
        tv_desc.setText(result.getDesc());
        Glide.with(getActivity()).load(result.getCover()).into(iv_cover);
        final SearchBean.ResultBean.PlaylinksBean playlinks = result.getPlaylinks();
        Gson gson=new Gson();
        String json = gson.toJson(playlinks);
        try {
            final JSONObject object=new JSONObject(json);
            Iterator<String> keys = object.keys();
            final List<String> texts=new ArrayList<>();
            while (keys.hasNext()){
                texts.add(keys.next());
            }
            for (int i = 0; i < texts.size(); i++) {
                final String s = object.optString(texts.get(i));
                tv= (TextView) inflater.inflate(R.layout.search_label_tv,flow_links,false);
                tv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.d("tag",s);
                        Intent intent=new Intent(getActivity(),WebViewActivity.class);
                        intent.putExtra(WebViewActivity.WEBKEY,s);
                        startActivity(intent);
//                        Intent it = new Intent(Intent.ACTION_VIEW, uri);
//                        startActivity(it);
                    }
                });
                tv.setText("播放地址"+(++i));
                flow_links.addView(tv);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        List<SearchBean.ResultBean.VideoRecBean> video_rec = result.getVideo_rec();
        for (int i = 0; i < video_rec.size(); i++) {
            View view1=inflater.inflate(R.layout.search_label_tv_iv,flow_rec,false);
            TextView tv1= (TextView) view1.findViewById(R.id.tv_search_label);
            tv1.setText(video_rec.get(i).getTitle());
            ImageView iv1= (ImageView) view1.findViewById(R.id.iv_search_label);
            Glide.with(getActivity()).load(video_rec.get(i).getCover()).into(iv1);
            flow_rec.addView(view1);
        }
        if (result.getAct_s().size()<1){
            ll.setVisibility(View.GONE);
        }
        tv_acts.setText("演员：");
        for (int i = 0; i < result.getAct_s().size(); i++) {
            View view1=inflater.inflate(R.layout.search_label_tv_iv,flow_rec,false);
            TextView tv1= (TextView) view1.findViewById(R.id.tv_search_label);
            tv1.setText(result.getAct_s().get(i).getName());
            ImageView iv1= (ImageView) view1.findViewById(R.id.iv_search_label);
            if (result.getAct_s().get(i).getImage()!=null){
                Glide.with(getActivity()).load(result.getAct_s().get(i).getImage()).into(iv1);
            }
            flow_acts.addView(view1);
        }
    }

}
